package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.R;
import com.demo.dao.UserDao;
import com.demo.domain.User;
import com.demo.service.UserService;
import com.demo.utils.Sendsms;
import com.demo.utils.ValidateCodeUtils;
import kotlin.jvm.internal.Lambda;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.HttpCookie;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送验证码
     *
     * @param user
     * @param session
     * @return
     */
    @Override
    public R<String> sendMsg(User user, HttpSession session) {
        //获取当前手机号
        String phone = user.getPhone();
        //判断当前手机号是否为空
        if (phone != null) {
            //获取验证码
            String code = String.valueOf(ValidateCodeUtils.generateValidateCode(4));
            log.info("验证码code为 {}", code);
            //将验证码存入session中
//            session.setAttribute(phone, code);

            //将验证码存入Redis中
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            return R.success("短信发送成功！");
        }
        return R.error("短信发送失败");

    }

    /**
     * 登录验证
     *
     * @param map
     * @param session
     * @return
     */
    @Override
    public R<User> login(@RequestBody Map map, HttpSession session) {

        //获取到map中的手机号
        String phone = map.get("phone").toString();
        //获取到map中的验证码
        String code = map.get("code").toString();

        //获取session中的验证码与前端传来的验证码进行比较
//        Object codeInSession = session.getAttribute(phone);

        //从Redis中获取验证码
        Object codeInSession = redisTemplate.opsForValue().get(phone);

        if (code != null && codeInSession.equals(code)) {
            //比对成功那就可以登录
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getPhone, phone);
            User user = this.getOne(lambdaQueryWrapper);
            //在判断当前手机号在数据库中是否存在，如果不存在那就保存至数据库中
            if (user == null) {
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                this.save(user);

            }
            session.setAttribute("user", user.getId());
            //删除原来session中的验证码
//            session.removeAttribute(phone);

            //如果登陆成功，则删除Redis中的验证码
            redisTemplate.delete(phone);

            return R.success(user);
        }
        return R.error("登陆失败");
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @Override
    public R<String> loginout(HttpSession session) {
        session.removeAttribute("user");
        return R.success("退出成功");
    }
}
